<?php
if( !defined('CORE') ) exit('Request Error!');


class ctl_user
{
    //登录
    public function login()
    {
        if (!isset($_SESSION['id']) || $_SESSION['id'] ==0)
        {
            if (!req::$posts)
            {
                tpl::display('login.tpl');
                exit();
            }
            $res = array("errcode"=>1,"msg"=>"");
            $email = trim(req::item("email"));
            $password = trim(req::item("password",'','password'));

            if (empty($email))
            {
                $res['msg'] = "邮箱不正确";
                exit(json_encode($res));
            }

            if(empty($password))
            {
                $res['msg'] = "密码不正确";
                exit(json_encode($res));
            }

            $password = md5($password);
            $login = db::get_one("SELECT * FROM `user` WHERE (`email`='{$email}' or `mobile`='{$email}') AND `password` = '{$password}'");
            if($login)
            {
                $res['errcode']=0;
                $res['msg']='登录成功';
                $res['url']='index.php';
                $_SESSION['id']=$login['id'];
                exit(json_encode($res));
            }
            else
            {
                $res['msg']='登录失败~请重新登录';
                exit(json_encode($res));
            }
        }
        else
        {
            $home_url = "index.php?ct=user&ac=user_center&id={$_SESSION['id']}";
            header('Location:'.$home_url);
        }
    }

    //退出
    public function logout()
    {
        $_SESSION['id']="";
    }

    //手机注册
    public function registered()
    {
        if(!req::$posts)
        {
            tpl::display('registered.tpl');
            exit();
        }
        $res = array('errcode'=>1,"msg"=>"");

        $password = trim(req::item('password','','password'));
        $mobile = trim(req::item('mobile','','phone'));

        if (empty($password))
        {
            $res['msg'] = '密码不正确';
            exit(json_encode($res));
        }

        if(empty($mobile))
        {
            $res['msg'] = '手机不正确';
            exit(json_encode($res));
        }

        $is_mobile = db::get_one("SELECT id FROM `user` WHERE `mobile`='{$mobile}'");
        if($is_mobile)
        {
            $res['msg'] = '该手机已注册';
            exit(json_encode($res));
        }


        $code = req::item("code");
        $asd = new cls_securimage();
        if(empty($code) || !$asd->check($code))
        {
            $res['msg']='验证码不正确';
            exit(json_encode($res));
        }

        req::$posts['password'] = md5(req::$posts['password']);
        req::$posts['register_time'] = date("Y-m-d H:i:s");
//        req::$posts['register_time'] = date("Y-m-d H:i:s");
        db::insert("user",req::$posts);
        $row = db::affected_rows();
        if ($row >0)
        {
            $res['errcode']=0;
            $res['msg']='注册成功';
            $res['url']='index.php?ct=user&ac=login';
            exit(json_encode($res));
        }
    }

    //邮箱注册
    public function mail_registered()
    {
        //模版页面
        if(!req::$posts)
        {
            tpl::display('mail.registered.tpl');
            exit;
        }

        $res = array('errcode'=>1,'msg'=>'');

        $password = trim(req::item('password', '', 'password'));
        $email = trim(req::item('email', '', 'email'));

        if(empty($email))
        {
            $res['msg']='邮箱不正确';
            exit(json_encode($res));
        }

        if(empty($password))
        {
            $res['msg']='密码不正确';
            exit(json_encode($res));
        }

        $is_email = db::get_one("SELECT id FROM `user` WHERE `email`='{$email}'");
        if ($is_email)
        {
            $res['msg']='该邮箱已注册';
            exit(json_encode($res));
        }

        $code = req::item('code', '');
        $vdimg = new cls_securimage();
        if( empty($code) || !$vdimg->check($code) )
        {
            $res['msg']='验证码不正确';
            exit(json_encode($res));
        }

        req::$posts['password'] = md5(req::$posts['password']);
        req::$posts['register_time'] = date('Y-m-d H:i:s');
        db::insert("user",req::$posts);
        $row = db::affected_rows();
        if ($row > 0)
        {
            $res['errcode']=0;
            $res['msg']='注册成功';
            $res['url']='index.php?ct=user&ac=login';
            exit(json_encode($res));
        }
    }

    //验证码类
    public function codes()
    {
        $vdimg = new cls_securimage(4, 150, 30, 3);
        $vdimg->show();
    }

    //个人中心
    public function user_center()
    {

        $id = $_SESSION['id'];
        $list = db::get_one("SELECT * FROM `user` WHERE `id` = '{$id}'");
        $list['headimgurl'] = empty($list['headimgurl'])?"static/images/0l0.jpg":$list['headimgurl'];

        tpl::assign("v",$list);
        tpl::display('user.center.tpl');
    }
}